﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using NSoup.Nodes;
using System.Collections;
using Com.Toook.AutoDownloadHtml.Entity;
using Com.Toook.AutoDownloadHtml.Util;
using Com.Toook.AutoDownloadHtml.SaveData;
using NSoup.Select;

namespace Com.Toook.AutoDownloadHtml.DownloadHtml.NewsHtml
{
    /// <summary>
    /// 搜狐解析新闻列表、提取新闻内容等公共方法父类
    /// </summary>
    public abstract class CommonSohuMethod : NewsAbstract
    {
        /// <summary>
        /// 取hrefStart开头的链接，并添加到this.sdNewsUrls(StringDictionary)
        /// </summary>
        /// <param name="links"></param>
        /// <param name="hrefStart"></param>
        protected void AddLinksToStringDictionary(Elements links, string hrefStart)
        {
            foreach(Element link in links)
            {
                if(link.Attr("href").StartsWith(hrefStart))
                {
                    if(!this.sdNewsUrls.ContainsKey(link.Attr("abs:href")))
                    {
                        if(!link.HasText)
                        {
                            continue;
                        }
                        if(link.Text().Length < 10)
                        {
                            continue;//标题太短的丢弃
                        }
                        this.sdNewsUrls.Add(link.Attr("abs:href"), link.Text());
                    }
                }
            }
        }

        /// <summary>
        /// 从每个类型、站点的新闻内容页面提取新闻保存到数据库
        /// </summary>
        /// <param name="typeSite">类型、站点</param>
        protected void CommonGetEachNewsAndSave(string typeSite)
        {
            Document docDetail;//新闻内容页面Document对象
            string sourceUrl;//新闻来源url

            foreach(DictionaryEntry d in this.sdNewsUrls)
            {
                sourceUrl = d.Key.ToString();
                docDetail = GetDocument(sourceUrl);
                if(docDetail == null)
                {
                    continue;//下载html失败
                }
                //删除新闻内容中的脚本
                Element content = docDetail.GetElementById("contentText");
                if(content == null)
                {
                    LogHelper.Log.Warn("docDetail.GetElementById('contentText') is null. url=" + sourceUrl);
                    continue;//没有内容
                }
                Elements script =   content.GetElementsByTag("script");
                if(script != null)
                {
                    script.Remove();
                }                
                Elements title = docDetail.GetElementsByTag("h1");
                if(title == null)
                {
                    LogHelper.Log.Warn("docDetail.GetElementsByTag('h1') is null. url=" + sourceUrl);
                }
                this.news = new News();
                this.news.Title = title.Text;
                this.news.Content = docDetail.GetElementById("contentText").Html();
                this.news.Id = SqliteHelper.GetNewsId();
                this.news.SourceUrl = sourceUrl;
                this.news.PubDate = SqliteHelper.GetNewsPubDateNow();
                this.news.TypeSite = typeSite;//"NativeSohu";

                this.SaveNewsContent();
            }
        }

        public CommonSohuMethod()
        {
        }
        public CommonSohuMethod(string url)
            : base(url)
        {
        }
        public CommonSohuMethod(string url, string charset)
            : base(url, charset)
        {
        }
    }
}
